<?php

/**
 * video actions.
 *
 * @package    vogue
 * @subpackage video
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class videoActions extends sfActions
{
  
  public function executeIndex(sfWebRequest $request)
  {
      $this->pager = Doctrine::getTable('Video')->getPager(array('keyword'=>$request->getParameter('keyword')), $request->getParameter('page'));
  }

  public function executeNew(sfWebRequest $request)
  {
      $this->form = new VideoForm();
  }
  
  
  public function executeLoadObjects(sfWebRequest $request)
  {
      $objects = array();
      $seasonId = $request->getParameter('seasonId');
      $objectType = $request->getParameter('objectType');
      
      $teamIds = Doctrine::getTable('BbSeasonTeams')->getTeamIds($seasonId);
      switch ($objectType)
      {
      	case 'match': $objects = Doctrine::getTable('BbMatch')->fetchArray(array('seasonId'=>$seasonId)); break;
      	case 'team': $objects = Doctrine::getTable('BbTeams')->fetchArray(array(), $teamIds); break;
      	case 'player': $objects = Doctrine::getTable('BbPlayers')->fetchArray(array(), $teamIds);break;
      }
      if (!sizeof($objects)) return sfView::NONE;

      $this->objects = $objects;
      $this->objectId = $request->getParameter('objectId') ? $request->getParameter('objectId') : 0;
  }
  

  public function executeCreate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST));
  
      $this->form = new VideoForm();
  
      $this->processForm($request, $this->form);
  
      $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
      $this->forward404Unless($video = Doctrine_Core::getTable('Video')->find(array($request->getParameter('id'))), sprintf('Object video does not exist (%s).', $request->getParameter('id')));
      $this->form = new VideoForm($video);
  }

  public function executeUpdate(sfWebRequest $request)
  {
      $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
      $this->forward404Unless($video = Doctrine_Core::getTable('Video')->find(array($request->getParameter('id'))), sprintf('Object video does not exist (%s).', $request->getParameter('id')));
      $this->form = new VideoForm($video);
  
      $this->processForm($request, $this->form);
  
      $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
      $request->checkCSRFProtection();
  
      $this->forward404Unless($video = Doctrine_Core::getTable('Video')->find(array($request->getParameter('id'))), sprintf('Object video does not exist (%s).', $request->getParameter('id')));
      $objectType = $video->getObjectType();
      
      $video->delete();
      
      $this->getUser()->setFlash('success', 'Видео амжилттай нэмэгдлээ.', true);
  
      $this->redirect('video/index?objectType='.$objectType);
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
      if ($form->isValid())
      {
        $video = $form->save();
  
        $this->getUser()->setFlash('success', 'Видео амжилттай нэмэгдлээ.', true);
        
        $this->redirect('video/new?objectType='.$video->getObjectType());
      }
  }


}
